package cn.iocoder.yudao.module.worker.controller.admin.goods;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.admin.goods.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.goods.WorkerGoodsDO;
import cn.iocoder.yudao.module.worker.service.goods.WorkerGoodsService;

@Tag(name = "管理后台 - 商品管理")
@RestController
@RequestMapping("/worker/goods")
@Validated
public class WorkerGoodsController {

    @Resource
    private WorkerGoodsService goodsService;

    @PostMapping("/create")
    @Operation(summary = "创建商品管理")
    @PreAuthorize("@ss.hasPermission('worker:goods:create')")
    public CommonResult<Long> createGoods(@Valid @RequestBody WorkerGoodsSaveReqVO createReqVO) {
        return success(goodsService.createGoods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品管理")
    @PreAuthorize("@ss.hasPermission('worker:goods:update')")
    public CommonResult<Boolean> updateGoods(@Valid @RequestBody WorkerGoodsSaveReqVO updateReqVO) {
        goodsService.updateGoods(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改商品状态")
    @PreAuthorize("@ss.hasPermission('worker:goods:update')")
    public CommonResult<Boolean> updateGoodsStatus(@Valid @RequestBody WorkerGoodsSaveReqVO reqVO) {
        goodsService.updateGoodsStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:goods:delete')")
    public CommonResult<Boolean> deleteGoods(@RequestParam("id") Long id) {
        goodsService.deleteGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:goods:query')")
    public CommonResult<WorkerGoodsRespVO> getGoods(@RequestParam("id") Long id) {
        WorkerGoodsDO goods = goodsService.getGoods(id);
        return success(BeanUtils.toBean(goods, WorkerGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品管理分页")
    @PreAuthorize("@ss.hasPermission('worker:goods:query')")
    public CommonResult<PageResult<WorkerGoodsRespVO>> getGoodsPage(@Valid WorkerGoodsPageReqVO pageReqVO) {
        PageResult<WorkerGoodsDO> pageResult = goodsService.getGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkerGoodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品管理 Excel")
    @PreAuthorize("@ss.hasPermission('worker:goods:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGoodsExcel(@Valid WorkerGoodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkerGoodsDO> list = goodsService.getGoodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品管理.xls", "数据", WorkerGoodsRespVO.class,
                        BeanUtils.toBean(list, WorkerGoodsRespVO.class));
    }

}